package com.example.todaynews.adapter;


import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.example.todaynews.R;

import com.example.todaynews.bean.NewsInfo;
import com.example.todaynews.bitmap.LruImageCache;

import java.util.List;

/**
 * Created by wj on 2016/8/19.
 */
public class NewsContentRecyclerviewAdapter extends RecyclerView.Adapter<NewsContentRecyclerviewAdapter.NewsViewHolder> {
    private List<NewsInfo> list;
    private Context mContext;
    private LayoutInflater inflater;

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public NewsContentRecyclerviewAdapter(Context mContext, List<NewsInfo> list) {
        this.list = list;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.activity_item_news, null);
        NewsViewHolder holder = new NewsViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final NewsViewHolder holder, final int position) {
        NewsInfo newsInfo = list.get(position);
        RequestQueue queue = Volley.newRequestQueue(mContext);
        LruImageCache lruImageCache = LruImageCache.instance();
        ImageLoader imageLoader = new ImageLoader(queue, lruImageCache);
        holder.tv_newsTitle.setText(newsInfo.getTitle());
        holder.img_newsPic1.setImageUrl(newsInfo.getThumbnail_pic_s(), imageLoader);
        holder.img_newsPic1.setDefaultImageResId(R.mipmap.pictures_no);
        holder.img_newsPic2.setImageUrl(newsInfo.getThumbnail_pic_s02(), imageLoader);
        holder.img_newsPic2.setDefaultImageResId(R.mipmap.pictures_no);
        holder.img_newsPic3.setImageUrl(newsInfo.getThumbnail_pic_s03(), imageLoader);
        holder.img_newsPic3.setDefaultImageResId(R.mipmap.pictures_no);
        holder.tv_newsDate.setText(newsInfo.getDate());
        holder.tv_newsType.setText(newsInfo.getRealtype());
        holder.imgBtn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyItemChanged(position);
            }
        });


        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class NewsViewHolder extends ViewHolder {
        TextView tv_newsTitle;
        NetworkImageView img_newsPic1;
        NetworkImageView img_newsPic2;
        NetworkImageView img_newsPic3;
        TextView tv_newsDate;
        TextView tv_newsType;
        ImageButton imgBtn_delete;

        public NewsViewHolder(View itemView) {
            super(itemView);
            tv_newsTitle = (TextView) itemView.findViewById(R.id.tv_item_newstitle);
            img_newsPic1 = (NetworkImageView) itemView.findViewById(R.id.img_newpic1);
            img_newsPic2 = (NetworkImageView) itemView.findViewById(R.id.img_newpic2);
            img_newsPic3 = (NetworkImageView) itemView.findViewById(R.id.img_newpic3);
            tv_newsDate = (TextView) itemView.findViewById(R.id.tv_item_newsdate);
            tv_newsType = (TextView) itemView.findViewById(R.id.tv_item_newstype);
            imgBtn_delete = (ImageButton) itemView.findViewById(R.id.imgBtn_itme_newsdelete);
        }
    }
}
